<template>
  <div class="main">
    <el-card class="box-card">
    <div class="top">
      登录
    </div>
    <div class="login">
      <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名：" prop="userName">
          <el-input v-model="ruleForm2.userName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="pass">
          <el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    </el-card>
  </div>

</template>
<script>
  export default {
    name:"login",
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      var validateUserName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          userName: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          userName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.HttpGet({url:"/api/login"},{userName:this.ruleForm2.userName,password:this.ruleForm2.pass})
              .then((data) => {
              debugger
                sessionStorage.setItem("user",data.data[0].user.cusername.toString());
                if(data){
                  this.router.push({ path: 'main' });
              }
              }).catch((err)=>{
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
  .main{
    width:400px;
    margin: 0 auto;
    margin-top: 10%;
  }
  .top{
    text-align: center;
    font-size: 30px;
    color: #333333;
    font-weight: 500;
    padding-bottom: 30px;
  }
  .login{
    padding-right: 30px;
  }


</style>
